package com.wawei.seeds.champion;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TextView;

import com.wawei.seeds.R;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Use the {@link ChampionFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ChampionFragment extends Fragment {
    @InjectView(R.id.tabHost) FragmentTabHost fragmentTabHost;

    /**
     * Use this factory method to create a new instance of
     * this fragment.
     *
     * @return A new instance of fragment ChampionFragment.
     */
    public static ChampionFragment newInstance() {
        return new ChampionFragment();
    }

    public ChampionFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_champion, container, false);
        ButterKnife.inject(this, view);
        initFragmentTabs();

        return view;
    }

    private void initFragmentTabs() {
        fragmentTabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);

        //Create the first tab
        RelativeLayout indicatorLayout = (RelativeLayout)LayoutInflater.from(getActivity())
                .inflate(R.layout.tab_indicator, fragmentTabHost.getTabWidget(), false);

        TextView tabTitle = (TextView)indicatorLayout.findViewById(R.id.tabTitle);
        tabTitle.setText(getResources().getString(R.string.tabtitle_tab1));
        fragmentTabHost.addTab(fragmentTabHost.newTabSpec(getResources().getString(R.string.tabspec_tab1))
                .setIndicator(indicatorLayout), ChampionBiddingTabFragment.class, null);

        //Create the second tab
        indicatorLayout = (RelativeLayout)LayoutInflater.from(getActivity())
                .inflate(R.layout.tab_indicator, fragmentTabHost.getTabWidget(), false);
        tabTitle = (TextView)indicatorLayout.findViewById(R.id.tabTitle);
        tabTitle.setText(getResources().getString(R.string.tabtitle_tab2));
        fragmentTabHost.addTab(fragmentTabHost.newTabSpec(getResources().getString(R.string.tabspec_tab2))
                .setIndicator(indicatorLayout), ChampionAchievementTabFragment.class, null);

        fragmentTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                for (int i = 0; i < fragmentTabHost.getTabWidget().getTabCount(); i++) {
                    ChampionFragment.this.changeTabSelected(i, false);
                }
                ChampionFragment.this.changeTabSelected(fragmentTabHost.getCurrentTab(), true);
            }
        });

        //The first tab (tab index equals zero) is the default selected tab, so we must make the selected status visible.
        fragmentTabHost.setCurrentTab(0);
        changeTabSelected(0, true);
    }

    private void changeTabSelected(int tabIndex, boolean selected){
        View indicator = fragmentTabHost.getTabWidget().getChildAt(tabIndex);
        TextView text = (TextView) indicator.findViewById(R.id.tabTitle);
        View triangle = indicator.findViewById(R.id.tabSelectedTriangle);
        View selectedLine = indicator.findViewById(R.id.tabSelectedLine);

        if(selected) {
            text.setTextColor(getResources().getColor(android.R.color.holo_blue_dark));
            triangle.setVisibility(View.VISIBLE);
            selectedLine.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_dark));
        }
        else{
            text.setTextColor(getResources().getColor(android.R.color.black));
            triangle.setVisibility(View.INVISIBLE);
            selectedLine.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        fragmentTabHost = null;
    }
}
